﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using System.Data;
using System.Text;

namespace ATW.MT.BaseManage
{
    public partial class TerminalManage : CommonPage
    {
        bus_T_AppUserAuthorised AppUser = new bus_T_AppUserAuthorised();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            try
            {
                string name = txtName.Value.Trim();
                string type = ddlUserType.SelectedValue;
                //user表
                DataTable dt = AppUser.AppUser(name,type, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        //修改
        protected void lbnModify_Click(object sender, EventArgs e)
        {
            //授权表
            DataTable da = AppUser.InformationCategory();
            rpList2.DataSource = da;
            rpList2.DataBind();

            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                DataTable dtResult = AppUser.AppUserAuthorised(strId);
                string Information = "";
                if (dtResult.Rows.Count > 0)
                {
                    for (var i = 0; i < dtResult.Rows.Count; i++)
                    {
                        Information += dtResult.Rows[i]["tauaTicId"].ToString() + "-";
                    }
                }
                this.hfInformation.Value = Information;
                this.hftauaTauId.Value = strId;
                StringBuilder sb = new StringBuilder();

                sb.Append("<script language='javascript'>");

                sb.Append("var divAdd = new jSharp.popUpDiv('divAdd');function Show(){divAdd.Show()} Show(); $('#hideAdd').click(function () {divAdd.Close();});modify();");

                sb.Append("</script>");

                ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", sb.ToString());


            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }
        protected void lbnAddUser_Click(object sender, EventArgs e)
        {//新增按钮
            if (!liAddUser.Visible)
            {
                liAddUser.Visible = true;
            }
            txtNewName.Text = "";
            txtPwd.Text = "";
            txtPwd2.Text = "";
        }
        protected void lbnSaveUser_Click(object sender, EventArgs e)
        {//保存按钮
            try
            {
                string account = txtNewName.Text.Trim();
                string truename = txtTrueName.Text.Trim();
                string pwd = txtPwd.Text.Trim();
                string pwd2 = txtPwd2.Text.Trim();

                if (account == "")
                {
                    Tips("请输入用户名！");
                    return;
                }
                if (pwd == "")
                {
                    Tips("请输入密码！");
                    return;
                }
                if (truename == "")
                {
                    Tips("请输入真实姓名！");
                    return;
                }
                if (pwd2 != pwd)
                {
                    Tips("两次输入的密码不一致！");
                    return;
                }
                if (!common.filterChar(account))
                {
                    Tips("用户名中含有危险字符！");
                    return;
                }
                if (!common.filterChar(pwd))
                {
                    Tips("密码中含有危险字符！");
                    return;
                }
                if (!common.filterChar(truename))
                {
                    Tips("真实姓名中含有危险字符！");
                    return;
                }
                if (AppUser.isUserExist(account))
                {
                    Tips("该账号已经存在！");
                    return;
                }
                int result = AppUser.AddUser(account, pwd, truename);
                if (result > 0)
                {
                    Tips("新增成功！");
                    BindData();
                    txtNewName.Text = "";
                    txtTrueName.Text = "";
                }
                else
                {
                    Tips("新增失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnChange_Click(object sender, EventArgs e)
        {
            string str = this.hfChangeInformation.Value.ToString();
            string[] strs = str.Split(',');
            string strId = this.hftauaTauId.Value.ToString();
            int result = 0;
            if (strs[0] != "")
            {
                result = AppUser.add(strId);
                foreach (var item in strs)
                {
                    result = AppUser.Change(strId, item);
                }
            }
            else
            {
                result = AppUser.delete(strId);
            }
            if (result > 0)
            {
                Tips("修改成功");
                BindData();
            }
            else
            {
                Tips("修改失败");
                return;
            }

        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        private void writeRes(string s)
        {
            Response.Clear();
            Response.Write(s);
            Response.End();
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}